package com.yisu.expressway.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;


import com.yisu.expressway.R;
import com.yisu.expressway.ui.TitlebarLayout;
import com.yisu.expressway.ui.loading.ActivityLoading;
import com.yisu.expressway.ui.loading.CustomProgressDialog;
import com.yisu.expressway.utils.DisplayUtil;
import com.yisu.expressway.utils.HBLog;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/13 0013.
 */
public class BaseActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "BaseActivity";

    protected TitlebarLayout mTitleBar;
    public static List<Activity> mActivityList = new ArrayList<Activity>();
    CustomProgressDialog mLoadingDialog;
    protected int NavigationBarHeight = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivityList.add(this);
        mLoadingDialog = new CustomProgressDialog(this);
        NavigationBarHeight = DisplayUtil.getNavigationBarSize(this).y;
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);

        mTitleBar = (TitlebarLayout)findViewById(R.id.titlebar);
        if (mTitleBar != null) {
            mTitleBar.setClickBackListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }

    }

    public void setTitle(String title) {
        if (mTitleBar != null)
            mTitleBar.setTitle(title);
    }

    public void setTitle(int strId) {
        if (mTitleBar != null)
            mTitleBar.setTitle(getString(strId));
    }

    @Override
    protected void onStart() {
        super.onStart();
        HBLog.d(TAG, "name is:  " + this.getClass().getName() + "-onStart.");

    }

    @Override
    protected void onRestart(){
        super.onRestart();
        HBLog.d(TAG, "name is:  " + this.getClass().getName() + "-onRestart-------------.");

    }

    @Override
    protected void onResume() {
        super.onResume();
        HBLog.d(TAG, "name is:  " + this.getClass().getName() + "-onResume.");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        HBLog.d(TAG, "name is:  " + this.getClass().getName() + "-onPause.");
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        HBLog.d(TAG, "name is:  " + this.getClass().getName() + "-onStop.");
    }

    @Override
    public void onBackPressed() {
        HBLog.d(TAG, "onBackPressed");
        this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        HBLog.d(TAG, "name is:  " + this.getClass().getName() + "-onDestroy.");
        mActivityList.remove(this);
        ButterKnife.unbind(this);
        mLoadingDialog = null;
    }

    /**
     * 显示默认loading
     */
    public void showLoading() {
//        ActivityLoading.showLoading(this, R.layout.activity_loading_simple);
        if(mLoadingDialog != null){
            mLoadingDialog.show();
            mLoadingDialog.showLoading();
        }
    }

    /**
     * 关闭loading
     */
    public void dismissLoading() {
//        ActivityLoading.dismiss(this);
        if(mLoadingDialog != null){
            mLoadingDialog.dismissLoading();
            mLoadingDialog.dismiss();
        }
    }



    @Override
    public void onClick(View v) {

    }

    /**
     * 关闭指定的activity
     *
     * @param actClass
     */
    public static void finishActivity(Class actClass) {
        for (Activity activity : mActivityList) {
            if (activity != null && activity.getClass() == actClass) {
                activity.finish();
            }
        }
    }

    public static void finishAllActivity() {
        for (Activity activity : mActivityList) {
            if (activity != null && !activity.isFinishing()) {
                activity.finish();
            }
        }
    }

    public static int getActivityAccount() {
        return mActivityList.size();
    }

    /**
     * 获取前一个activity类型
     *
     * @return prev activity class or null
     */
    protected final Class prevActivity() {
        if (mActivityList != null && mActivityList.size() > 1) {
            Activity act = mActivityList.get(mActivityList.size() - 2);
            if (act != null) {
                return act.getClass();
            }
        }
        return null;
    }

    public void startTargetActivity(Class targetClass) {
        Intent mIntent = new Intent(getApplicationContext(), targetClass);
        startActivity(mIntent);
    }

}
